#include "gamecontral.h"

GameContral::GameContral(QObject *parent) : QObject(parent)
{
    //初始化玩家
    initPlayer();
}

void GameContral::initPlayer()
{
    //开始初始化玩家
    robotPlayerLeft = new RobotPlayer("机器人A",this);
    robotPlayerRight = new RobotPlayer("机器人B",this);
    userPlayer = new UserPlayer("我自己",this);

    //初始化性别
    robotPlayerLeft->setSex(Player::Sex::Man);
    robotPlayerRight->setSex(Player::Sex::Man);
    userPlayer->setSex(Player::Sex::Man);

    //初始化前后玩家
    robotPlayerLeft->setPrePlayer(robotPlayerRight);
    robotPlayerLeft->setNextPlayer(userPlayer);

    robotPlayerRight->setPrePlayer(userPlayer);
    robotPlayerRight->setNextPlayer(robotPlayerLeft);

    userPlayer->setPrePlayer(robotPlayerLeft);
    userPlayer->setNextPlayer(robotPlayerRight);

    //初始化当前玩家为自己
    currentPlayer = userPlayer;


}

Player *GameContral::getCurrentPlayer()
{
    return currentPlayer;
}

void GameContral::setCurrentPlayer(Player*player)
{
    currentPlayer =  player;
}

void GameContral::setPendCards(Cards *cards)
{
    PendCards.addCards(*cards);
}

Cards GameContral::getPendCards()
{
    return PendCards;
}

void GameContral::setPendPlayer(Player *player)
{
    PendPlayer = player;
}

Player *GameContral::getPendPlayer()
{
    return PendPlayer;
}

void GameContral::initAllCards()
{
    //根据点数和花色进行初始化
    for(int point  = CardPoint::C_3;point <CardPoint::C_SJ;point++)
    {
        for(int suit = SUIT::S_clubs;suit<SUIT::S_end;suit++)
        {
            Card card((CardPoint)point,(SUIT)suit);
            allCards.addCard(card);
        }
    }

    //单独初始化大小王
    Card sj(CardPoint::C_SJ,SUIT::S_begin);
    Card bj(CardPoint::C_BJ,SUIT::S_begin);

    allCards.addCard(sj);
    allCards.addCard(bj);
}

Card GameContral::takeRandomOneCard()
{
    return allCards.getRandomCard();
}

Cards GameContral::takeLastThreeCards()
{
    return allCards;
}

void GameContral::resetCards()
{
    allCards.clear();
    userPlayer->clearCards();
    robotPlayerLeft->clearCards();
    robotPlayerRight->clearCards();
    currentPlayer = nullptr;
    PendCards.clear();
}

void GameContral::becomeLander(Player *player)
{
    player->setRole(Player::Role::Lander);
    player->getPrePlayer()->setRole(Player::Role::Farmer);
    player->getNextPlayer()->setRole(Player::Role::Farmer);
    setCurrentPlayer(player);
}

void GameContral::callLander(Player *player)
{
    player->CallLander();
}






